package com.nsxy.manager.common.service;

import java.util.Collection;

import javax.annotation.Resource;

import org.apache.shiro.mgt.RealmSecurityManager;
import org.apache.shiro.realm.Realm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

/**
 * 解决aop 和 shiro集成后，部分事物失效的bug
 * @author 潜行的青衣
 * @see
 */
@Component
public class ShiroInitListener implements ApplicationListener<ContextRefreshedEvent> {

	@Autowired
	private Collection<Realm> realms;
	
	@Resource
	private RealmSecurityManager securityManager;
	
	/* (non-Javadoc)
	 * @see org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent)
	 */
	@Override
	public void onApplicationEvent(ContextRefreshedEvent arg0) {
		if(this.realms != null && this.securityManager != null) {
			securityManager.setRealms(realms);
		}
	}

}
